<? include("/inc/header.html") {} ?>
<link rel="stylesheet" href="static/uploadify/uploadify.css" type="text/css"></link>
<script type="text/javascript" src="static/uploadify/jquery.uploadify.min.js"></script>
<script type="text/javascript" src="static/utf8-jsp/ueditor.config.js"></script>
<script type="text/javascript" src="static/utf8-jsp/ueditor.all.js"></script>
<script type="text/javascript" src="static/utf8-jsp/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" src="static/utf8-jsp/third-party/video-js/video.js"></script>
<script type="text/javascript" src="static/My97DatePicker/WdatePicker.js"></script>
<div class="col-lg-12" style="overflow: auto;height: 500px;">
	<div class="panel panel">
		<div class="panel-body pan">
			<div class="form-body pal">
				<div>
					<label for="inputUsername" class="col-md-3 control-label">消息标题（只允许汉字、英文字母、数字及下划线,20个字符以内）<span
						class='require'>*</span> </label>
					<div class="col-md-9">
						<div class="input-icon">
							<input id="title" type="text" class="form-control" />
						</div>
					</div>
				</div>
				<div>
					<label for="inputUsername" class="col-md-3 control-label">封面图片
						<span class='require'>*</span> </label>
					<div class="col-md-9">
						<div >
							<div style="display:inline " id="cover"></div>
							<input type="button" class="btn btn-primary" style="width: 70px;height: 30px;padding: 0;background-color: #4cae4c;border-color: #4cae4c;" onclick="upImages()" value="上传图片"  />
							<textarea id="uploadImage" name="uploadImage" style="display: none;"></textarea>
							<input id="coverImg" type="hidden" class="form-control" value="" />
						</div>
					</div>
				</div>
				<div>
					<label for="inputUsername" class="col-md-3 control-label">消息内容（10000个字符以内）
						<span class='require'>*</span> </label>
					<div class="col-md-9">
						<div class="input-icon">
							<script id="content" name="content" type="text/plain" ></script>
							 <script type="text/javascript">
						        var text = UE.getEditor('content',{ initialFrameHeight:370 });
						    </script>
						</div>
					</div>
				</div>
			</div>
		</div>
		<input id="id" type="hidden" value="${id}"/>
	</div>
</div>
<script>
	$(function() {
		_editor = UE.getEditor('uploadImage');
	    _editor.ready(function (){
	        //设置编辑器不可用(事实上不可以设置不可用...所以注释掉,以观后效)
	        _editor.setDisabled();
	        //隐藏编辑器,因为只使用上传功能
	        _editor.hide();
	        //侦听图片上传
	  	 	_editor.addListener('beforeInsertImage',function(t,arg){
	  	 			for(var i=0;i<arg.length;i++){
	  	 				var urlPath = arg[i].src;
	  	 				$("#coverImg").val(urlPath);
						$("#cover").html("<a href="+urlPath+" download='w3logo'><img src='"+urlPath+"' width='30' height='30' /></a>");
	  	 			}
	        });
	        //侦听文件上传
		   	 _editor.addListener('afterUpfile',function(t,arg){
		   	 	
		   	 });
	   });
		init();
	});
function init(){
	if($("#id").val() != null 
		&& $("#id").val() != ''
		&& $("#id").val() != 'null'
		&& $("#id").val() != 'undefined'
		&&$("#id").val() != undefined){
		$.ajax({
			type : "post",
			url : "./messageFind.do",
			data : {
				"id" : $("#id").val(),	
			},
			dataType : "json",
			success : function(data) {
				$("#title").val(data[0].title);
				
				text.ready(function() {
				    text.setContent(data[0].content);  //赋值给UEditor
				});
				/* text.addListener("ready", function () {
			　　　　// editor准备好之后才可以使用
			　　　　text.setContent(data[0].content);
			　　}); */
				
				$("#coverImg").val(data[0].cover);
				$("#cover").html("<a href="+data[0].cover+" download='w3logo'><img src='"+data[0].cover+"' width='30' height='30' /></a>");
			},
			error : function(data) {
				alert("请求失败");
			}
		});
	}
}

function save(){
	var id;
	if($("#id").val() != null 
		&& $("#id").val() != ''
		&& $("#id").val() != 'null'
		&& $("#id").val() != 'undefined'
		&&$("#id").val() != undefined){
		id=$("#id").val();
	}
	
	
	
	var title=$("#title").val(); //    
    var re =/^[\u4e00-\u9fa5_a-zA-Z0-9]{1,20}$/;
	if (!re.test(title)) {
		layer.msg('消息标题:只允许汉字、英文字母、数字及下划线,限制20个字符以内。');
		return null;
	}
	if($.trim($("#coverImg").val())==''){
		layer.msg('请上传封面图片');
		return null;
	}
	
	 var content = text.getContent()
	 content = content.replace(/<[^>]*>|<\/[^>]*>/g, "");
	 if(content.length>10000 || content.length<1){
	 	layer.msg('请输入消息内容：10000字以内');
	 	return null;
	 }
	 
	$.ajax({
		type : "post",
		url : "./messageAddUpdate.do",
		data : {
			"id" : id,
			"title" : $("#title").val(),
			"content" : text.getContent(),
			"cover" : $("#coverImg").val(),
		},
		dataType : "json",
		success : function(data) {
			if(data[0].result == 'success'){
				var index = parent.layer.getFrameIndex(window.name);
				window.parent.get();
				parent.layer.msg('提交成功');
				parent.layer.close(index);
			}else{
				parent.layer.msg(data[0].msg);
			}
		},
		error : function(data) {
			alert("请求失败");
		}
	});
}
//上传图片
function upImages() {
	var myFiles = _editor.getDialog("insertimage");
  		myFiles.open();
}
 //上传文件
function upFiles() {
	var myFiles = _editor.getDialog("attachment");
   	myFiles.open();
}
</script>